08. PD Controller Solution

def run(robot, tau_p, tau_d, n=100, speed=1.0):
    x_trajectory = []
    y_trajectory = []
    prev_cte = robot.y
    for i in range(n):
        cte = robot.y
        diff_cte = cte - prev_cte
        prev_cte = cte
        steer = -tau_p * cte - tau_d * diff_cte
        robot.move(steer, speed)
        x_trajectory.append(robot.x)
        y_trajectory.append(robot.y)
    return x_trajectory, y_trajectory

This is very similar to the P controller. We've added the prev_cte variable which is assigned to the previous CTE and diff_cte , the difference between the current CTE and previous CTE. We then put it all together with the new tau_d parameter to calculate the new steering value, -tau_p * cte - tau_d * diff_cte .

As we can see from the above image the PD controller performs much better!

PD Controller Solution - Artificial Intelligence for Robotics